// pc/07my/balance.js
var co = require('../../js/common.js')
var app = getApp(), _this
Page({
   data: {
      balance: 0.00,
      rechargeViewShow: 0
   },

   onLoad: function (options) {
      _this = this
      this.setData({ balance: app.aData.userInfo.balance })
   },

   recharge: function () {
      this.setData({ rechargeViewShow: this.data.rechargeViewShow ? 0 : 1 })
   },

   pay: function (e) {
      //金额为上门费或维修费
      var param = {
         openid: app.aData.userInfo.openid,
         appid: app.aData.app.appid,
         key: 'WqSeufzIsPDd4aGhGj97B3Il5g54bVDb',
         notify_url: 'http://api.ls293.cc/public/weixinpay/notify.php',
         mch_id: '1489661752',
         money: e.detail.value.money,
         abstract: '门业维修平台充值'
      }

      co.getShareApi('weixin/pay/createParam', param, function (res) {
         res.data.success = function (result) {
            console.log('【调起支付成功res】', result)
            _this.setData({ rechargeViewShow: 0 })
            _this.savePay(param.money, param.abstract)
            _this.updateUserBalance(param.money)
         }
         res.data.fail = function (result) {
            console.log('【调起支付失败res】', result)
         }
         //调起微信支付
         wx.requestPayment(res.data)
      })
   },
   //保存支付
   savePay: function (money, _abstract) {
      var params = {
         order_id: 0,
         appid: app.aData.app.appid,
         org: 'repair',
         money: money,
         abstract: _abstract,
         isBalancePay: 1
      }
      var keys = ['avatarUrl', 'unionid', 'openid', 'nickName', 'real_name']
      params = co.addValueForKey(params, app.aData.userInfo, keys)
      co.getShareApi('finance/Fi/paySave', params, function (res) {
         console.log('支付保存结果', res)
      })
   },

   updateUserBalance: function (money) {
      var params = {
         unionid: app.aData.userInfo.unionid,
         money: money,
      }
      co.getShareApi('user/Mg/balanceChange', params, function (res) {
         console.log('用户余额更新结果', res)
         _this.setData({ balance: res.data.balance })
         app.aData.userInfo.balance = res.data.balance
         wx.setStorageSync('userInfo', app.aData.userInfo)
      })
   },

   toBalanceList: function () {
      wx.navigateTo({ url: '/pc/07my/balanceList' })
   }
})